package com.maszy.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.maszy.bean.Car;
import com.maszy.bean.Dept;
import com.maszy.bean.DoorStore;
import com.maszy.bean.GoodPlan;
import com.maszy.bean.Product;
import com.maszy.service.CarService;
import com.maszy.service.DeptService;
import com.maszy.service.ProductService;

public class ProductServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	ProductService service = new ProductService();
	String view = new String(); // 页面路径

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ProductServlet servlet = new ProductServlet();
		RequestDispatcher rd = null;
		String method = request.getParameter("method");
	if ("queryAll".equals(method)) {
			// 查询出全部数据
			servlet.queryAll(request, response);
			view = "/background/product/product.jsp";
		} else if ("insert".equals(method)) {
			// 添加新记录
			servlet.add(request, response);
			servlet.queryAll(request, response);
			view = "/background/product/product.jsp";
		} else if ("update".equals(method)) {
			// 修改记录
			servlet.update(request, response);
			servlet.queryAll(request, response);
			view = "/background/product/product.jsp";
		//view = "/background/product?method=updateForward&Action_Id=902";
		} else if ("delete".equals(method)) {
			// 删除记录
			servlet.delete(request, response);
			servlet.queryAll(request, response);
		} else if ("updateForward".equals(method)) {
			// 进入修改页面
			servlet.queryForOne(request, response);
			view = "/background/product/update_product.jsp";
		} else if ("insertForward".equals(method)) {
			// 进入添加页面
			view = "/background/product/add_product.jsp";
		} 

		try {
			rd = this.getServletContext().getRequestDispatcher(view);
			rd.forward(request, response);

		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("product_Id");
		if(!"".equals(id)&&id!=null){
			Product obj=new Product();
			obj.setProduct_Id(Long.parseLong(id));
			service.delete(obj);
		}
	}

	private void queryForOne(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		String id=request.getParameter("product_Id");
		if(!"".equals(id)&&id!=null){
			Product obj=new Product();
			obj.setProduct_Id(Long.parseLong(id));
			obj=service.queryById(obj);
			request.setAttribute("obj", obj);
		}
		
		DeptService deptService= new DeptService();
		List list=deptService.queryAll();
		request.setAttribute("list", list);
	}

	private void update(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Product obj=new Product();
		String id=request.getParameter("product_Id");
		obj.setProduct_Name(request.getParameter("Product_Name"));
		obj.setProduct_Unit(request.getParameter("Product_Unit"));
		obj.setProduct_Dept_Id(Integer.parseInt(request.getParameter("Product_Dept")));
		obj.setProduct_Specification(request.getParameter("Product_Specification"));
		obj.setProduct_Status(request.getParameter("Product_Status"));
		obj.setProduct_Remark(request.getParameter("Product_Remark"));
		obj.setProduct_Sort(Integer.valueOf(request.getParameter("Product_Sort")));
		if(!"".equals(id)&&id!=null){		
			obj.setProduct_Id(Long.parseLong(id));	
			service.update(obj);
		}else{
			service.add(obj);
		}
	}

	private void queryAll(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
//		List list = service.queryAll();
//		request.setAttribute("list", list);
		DeptService deptService=new DeptService();
		List<Dept> deptList=deptService.queryAll();
		List<GoodPlan> goodPlanList=new ArrayList<GoodPlan>();	
		List<Product> proList=service.queryAll();
		for(int i=0;i<deptList.size();i++){
			int dept_id=deptList.get(i).getDept_id();
			List<Product> tempList=new ArrayList<Product>();
			for(int j=0;j<proList.size();j++){
				if(dept_id==proList.get(j).getProduct_Dept_Id()){
					tempList.add(proList.get(j));
				}
			}
			GoodPlan goodPlan=new GoodPlan();
			goodPlan.setDept_Name(deptList.get(i).getDept_name());
			goodPlan.setProductList(tempList);
			goodPlanList.add(goodPlan);
		}
		request.setAttribute("goodPlanList", goodPlanList);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		Product obj = new Product();
		obj.setProduct_Name(request.getParameter("Product_Name"));
		obj.setProduct_Unit(request.getParameter("Product_Unit"));
		obj.setProduct_Remark(request.getParameter("Product_Remark"));
		service.add(obj);	
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}